package com.solution.admin.customer.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.solution.admin.customer.entity.CompanyExpressConfigAddressEntity;
import com.solution.admin.customer.entity.CompanyExpressConfigEntity;
import com.solution.admin.customer.service.CompanyExpressConfigService;
import com.solution.common.constants.PublicStatus;
import com.solution.common.controller.BaseController;
import com.solution.common.entity.BaseReturn;
import com.solution.common.page.JSONGrid;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.utils.string.StringUtil;

/**
 * 公司常用物流配置web控制层
 * @author solin
 * @date 2018-03-22 18:49:13
 */
@RequestMapping("/companyExpressConfig")
@Controller
public class CompanyExpressConfigController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(CompanyExpressConfigController.class);

	@Autowired
	private CompanyExpressConfigService companyExpressConfigService;
	
	/**
	 * 跳转公司常用物流配置查询页面
	 * @param CompanyExpressConfig 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author solin
 	 * @date 2018-03-22 18:49:13
	 */
	@RequestMapping("/toListCompanyExpressConfigPage")
	public String toListCompanyExpressConfigPage(CompanyExpressConfigEntity companyExpressConfig, HttpServletRequest request) {
		return "customer/companyExpressConfig_list";
	}

	/**
	 * 分页查询公司常用物流配置列表
	 * @param companyExpressConfig 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author solin
 	 * @date 2018-03-22 18:49:13
	 */
	@RequestMapping("/listCompanyExpressConfigPage")
	@ResponseBody
	public JSONGrid listCompanyExpressConfigPage(CompanyExpressConfigEntity companyExpressConfig, HttpServletRequest request){
		int pageNum=Integer.parseInt(request.getParameter("page").toString());
		int numPerPage=Integer.parseInt(request.getParameter("rows").toString());
		PageParam pageParam = super.getPageParam(request);
		pageParam.setPageNum(pageNum);
		pageParam.setNumPerPage(numPerPage);
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		try {
			PageBean pageBean = companyExpressConfigService.listCompanyExpressConfigPage(pageParam, paramMap);
			return new JSONGrid(pageBean.getRecordList(),pageBean.getTotalCount());
		} catch (Exception e) {
			handleException("分页查询公司常用物流配置失败", logger, e);
		}
		return new JSONGrid();
	}
	
	/**
	 * 查询公司常用物流配置列表
	 * @param companyExpressConfig
	 * @param request
	 * @return
	 * @author: solin
	 * @date: 2018年3月23日 下午3:58:09
	 */
	@RequestMapping("/listCompanyExpressConfig")
	@ResponseBody
	public Map<String, Object> listCompanyExpressConfig(CompanyExpressConfigEntity companyExpressConfig, HttpServletRequest request){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		try {
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("companyId", request.getParameter("companyId"));
			List<CompanyExpressConfigEntity> companyExpressConfigList = companyExpressConfigService.listCompanyExpressConfig(paramMap);
			resultMap.put("data", companyExpressConfigList);
			resultMap.put(PublicStatus.RESULT_CODE, companyExpressConfigList != null?"1":"0");
			resultMap.put(PublicStatus.RESULT_MSG, companyExpressConfigList != null?"操作成功":"操作失败");
		} catch (Exception e) {
			handleException("查询公司常用物流配置失败", logger, e);
			resultMap.put(PublicStatus.RESULT_CODE, "0");
			resultMap.put(PublicStatus.RESULT_MSG, "操作失败");
		}
		
		return resultMap;
	}
	
	
	/**
	 * 跳转新增页面
	 * @return string 跳转页面路径
	 * @author solin
 	 * @date 2018-03-22 18:49:13
	 */
	@RequestMapping("/toAddCompanyExpressConfig")
	public String toAddCompanyExpressConfig(HttpServletRequest request) {
		request.setAttribute("opFlag", "add");
		return "customer/companyExpressConfig_form";
	}

	/**
	 * 跳转编辑页面
	 * @return string 跳转页面路径
	 * @author solin
 	 * @date 2018-03-22 18:49:13
	 */
	@RequestMapping("/toUpdateCompanyExpressConfig")
	public String toUpdateCompanyExpressConfig(HttpServletRequest request) {
		// 编辑操作
		request.setAttribute("opFlag", "edit");
		long id = Long.valueOf(request.getParameter("id"));
		try {
			CompanyExpressConfigEntity companyExpressConfig = companyExpressConfigService.getCompanyExpressConfigById(id);
			request.setAttribute("companyExpressConfig", companyExpressConfig);
		} catch (Exception e) {
			handleException("跳转编辑公司常用物流配置页面错误", logger, e);
		}
		return "customer/companyExpressConfig_form";
	}


	/**
	 * 新增或保存公司常用物流配置
	 * @param request 请求对象
	 * @param companyExpressConfigEntity 公司常用物流配置实体
	 * @return
	 * @author solin
 	 * @date 2018-03-22 18:49:13
	 */
	@RequestMapping("/saveOrUpdateCompanyExpressConfig")
	@ResponseBody
	public Map<String, Object> saveCompanyExpressConfig(HttpServletRequest request, Long companyId) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		try {
			String data = request.getParameter("data");
			if (StringUtil.isNotEmpty(data)) {
				JSONArray configList = JSONArray.parseArray(data);
				List<CompanyExpressConfigEntity> companyExpressConfigList = new ArrayList<CompanyExpressConfigEntity>();
				// 普通物流-免运费 地址数据
				List<CompanyExpressConfigAddressEntity> companyExpressConfigAddressList1 = new ArrayList<CompanyExpressConfigAddressEntity>();
				// 快递物流-免运费 地址数据
				List<CompanyExpressConfigAddressEntity> companyExpressConfigAddressList2 = new ArrayList<CompanyExpressConfigAddressEntity>();
				for (int i = 0; i < configList.size(); i++) {
					CompanyExpressConfigEntity expressConfig = new CompanyExpressConfigEntity();
					expressConfig.setCompanyId(companyId);
					int expressType = configList.getJSONObject(i).getIntValue("expressType");
					expressConfig.setExpressType(expressType);
					companyExpressConfigList.add(expressConfig);
					
					//如果是 普通物流-免运费 或者  快递物流-免运费
					if (expressType == 1 || expressType == 3) {
						String address = configList.getJSONObject(i).getString("address");
						if (StringUtil.isNotEmpty(address)) {
							String[] addrArray = address.split(",");
							for(String addr : addrArray) {
								CompanyExpressConfigAddressEntity companyExpressConfigAddress = new CompanyExpressConfigAddressEntity();
								companyExpressConfigAddress.setAddressId(Long.parseLong(addr));
								companyExpressConfigAddress.setCompanyId(companyId);
								if (expressType == 1) {
									companyExpressConfigAddressList1.add(companyExpressConfigAddress);
								} else {
									companyExpressConfigAddressList2.add(companyExpressConfigAddress);
								}
							}
						}
					}
				}
				
				result = (int)companyExpressConfigService.saveCompanyExpressConfig(companyExpressConfigList, 
						companyExpressConfigAddressList1, companyExpressConfigAddressList2);
				
				resultMap.put(PublicStatus.RESULT_CODE, result >= 1?"1":"0");
				resultMap.put(PublicStatus.RESULT_MSG, result >= 1?"操作成功":"操作失败");
			}
		} catch (Exception e) {
			handleException("添加公司常用物流配置失败", logger, e);
			resultMap.put(PublicStatus.RESULT_CODE, "0");
			resultMap.put(PublicStatus.RESULT_MSG, "操作失败");
		}
		return resultMap;
	}
	
}
